import Clipboard from 'clipboard'
import { vueInstance } from '@/main'

/* 复制方法 */
export function copyFuc () {
  var clipboard = new Clipboard('.copyBtn')
  clipboard.on('success', e => {
    vueInstance.$message.success('复制成功')
    clipboard.destroy()
  })
  clipboard.on('error', e => {
    vueInstance.$message.warning('复制失败,请手动复制!')
  })
}

export const openWindow = (url, name = '_self') => {
  window.open(url, name)
}

/**
 * 查询条件转为导出文件link所需条件
 * @param Object 查询条件对象
 * @returns `a=1&b=2`
*/
export const transferToOutputCondition = (data = {}) => {
  return Object.keys(data).map(key => {
    return `${key}=${data[key] || ''}`
  }).join('&')
}

/**
 * 手机号脱敏
*/
export function phoneConceal (phone) {
  if (!phone) return
  if (phone.length < 11) return phone
  return phone.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
}

/**
 * 身份证号脱敏
*/
export function idNumberConceal(idNumber) {
  if (!idNumber) return;
  return idNumber.replace(/^(.{4}).*(.{2})$/, '$1**************$2');
}